package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.android.material.appbar.MaterialToolbar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.adapter.RecyclerAdapter;
import cn.lrwjz.androidguide.adapter.TouchHelperAdapter;
import cn.lrwjz.androidguide.helper.RecyclerItemTouchHelper;
import cn.lrwjz.androidguide.model.RecyclerItem;

public class RecyclerViewActivity extends AppCompatActivity {
    private Random random = new Random();
    private ItemTouchHelper touchHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("RecyclerView 示例");
        }

        // 设置线性布局 RecyclerView
        RecyclerView linearRecyclerView = findViewById(R.id.linearRecyclerView);
        linearRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecyclerAdapter linearAdapter = new RecyclerAdapter(createItems(), false);
        linearAdapter.setOnItemClickListener((item, position) -> 
            Toast.makeText(this, "线性布局: " + item.getTitle(), Toast.LENGTH_SHORT).show());
        linearRecyclerView.setAdapter(linearAdapter);

        // 设置网格布局 RecyclerView
        RecyclerView gridRecyclerView = findViewById(R.id.gridRecyclerView);
        gridRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        RecyclerAdapter gridAdapter = new RecyclerAdapter(createItems(), false);
        gridAdapter.setOnItemClickListener((item, position) -> 
            Toast.makeText(this, "网格布局: " + item.getTitle(), Toast.LENGTH_SHORT).show());
        gridRecyclerView.setAdapter(gridAdapter);

        // 设置瀑布流布局 RecyclerView
        RecyclerView staggeredRecyclerView = findViewById(R.id.staggeredRecyclerView);
        staggeredRecyclerView.setLayoutManager(
            new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        RecyclerAdapter staggeredAdapter = new RecyclerAdapter(createItems(), true);
        staggeredAdapter.setOnItemClickListener((item, position) -> 
            Toast.makeText(this, "瀑布流布局: " + item.getTitle(), Toast.LENGTH_SHORT).show());
        staggeredRecyclerView.setAdapter(staggeredAdapter);

        // 设置滑动删除 RecyclerView
        RecyclerView swipeRecyclerView = findViewById(R.id.swipeRecyclerView);
        swipeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        TouchHelperAdapter swipeAdapter = new TouchHelperAdapter(createItems());
        swipeAdapter.setOnItemClickListener((item, position) -> 
            Toast.makeText(this, "滑动删除: " + item.getTitle(), Toast.LENGTH_SHORT).show());
        swipeRecyclerView.setAdapter(swipeAdapter);
        
        // 设置滑动删除功能
        RecyclerItemTouchHelper recyclerItemTouchHelper = new RecyclerItemTouchHelper(swipeAdapter, false, true);
        // ItemTouchHelper 是一个帮助类，用于处理 RecyclerView 中的手势操作
        ItemTouchHelper swipeTouchHelper = new ItemTouchHelper(recyclerItemTouchHelper);
        swipeTouchHelper.attachToRecyclerView(swipeRecyclerView);

        // 设置拖动排序 RecyclerView
        RecyclerView dragRecyclerView = findViewById(R.id.dragRecyclerView);
        dragRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        TouchHelperAdapter dragAdapter = new TouchHelperAdapter(createItems());
        dragAdapter.setOnItemClickListener((item, position) -> 
            Toast.makeText(this, "拖动排序: " + item.getTitle(), Toast.LENGTH_SHORT).show());
        dragRecyclerView.setAdapter(dragAdapter);
        
        // 设置拖动排序功能
        touchHelper = new ItemTouchHelper(new RecyclerItemTouchHelper(dragAdapter, true, false));
        touchHelper.attachToRecyclerView(dragRecyclerView);
    }

    private List<RecyclerItem> createItems() {
        List<RecyclerItem> items = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            items.add(new RecyclerItem(
                "项目 " + i,
                "这是项目 " + i + " 的描述文本",
                android.R.drawable.ic_menu_gallery,
                getRandomHeight()
            ));
        }
        return items;
    }

    private int getRandomHeight() {
        return 300 + random.nextInt(300); // 300-600之间的随机高度
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 